package com.example.demo3.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.DictData;
import com.example.demo3.entity.DictType;
import com.example.demo3.mapper.DictDataMapper;
import com.example.demo3.mapper.DictTypeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DictTypeService extends ServiceImpl<DictTypeMapper, DictType> {

    @Resource
    private DictDataService dictDataService;

    @Resource
    private DictTypeMapper dictTypeMapper;

    @Resource
    private DictDataMapper dictDataMapper;

    @Transactional
    public ResponseMessage saveDictType(DictType dictType){
        dictType.setCompanyCode("1");
        dictType.setCreateUser("wcm");
        dictType.setDelFlag(0);
        dictType.setCreateTime(new Date());
       // List<DictType> list = list(Wrappers.<DictType>lambdaQuery().eq(DictType::getCompanyCode,dictType.getCompanyCode()).and(i->i.eq(DictType::getDictName, dictType.getDictName()).or().eq(DictType::getDictType,dictType.getDictType())));
       List<DictType> checkDictType = list(Wrappers.<DictType>lambdaQuery().eq(DictType::getCompanyCode,dictType.getCompanyCode()).eq(DictType::getDictType,dictType.getDictType()).eq(DictType::getDelFlag,0));
       List<DictType> checkDictName = list(Wrappers.<DictType>lambdaQuery().eq(DictType::getCompanyCode,dictType.getCompanyCode()).eq(DictType::getDictName,dictType.getDictName()).eq(DictType::getDelFlag,0));
       String msg = "";
       //判断字典类型编号是否重复
        if(CollectionUtils.isNotEmpty(checkDictType)){
            msg = "字典类型编号已重复！";
            return ResponseMessage.error(msg);
        }
        //判断字典类型名称是否重复
        if(CollectionUtils.isNotEmpty(checkDictName)){
            msg = "字典类型名称已重复！";
            return ResponseMessage.error(msg);
        }
        save(dictType);
        msg = "新增"+dictType.getDictName()+"成功!";
        return ResponseMessage.ok(null,msg);
    }

    public List<DictType> getAllType(){
        QueryWrapper<DictType> queryWrapper=new QueryWrapper<DictType>()
                .select("ID","DICT_NAME","DICT_TYPE")
                .eq("DEL_FLAG",0)
                .orderByAsc("CREATE_TIME");
        List<DictType> list = dictTypeMapper.selectList(queryWrapper);
        return list;
    }

    public DictType getDciTypeById(String id){
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<DictType>()
                .eq("ID",id);
        DictType dictType = dictTypeMapper.selectOne(queryWrapper);
        return dictType;
    }

    public DictType getDciTypeByType(String type){
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<DictType>()
                .eq("DICT_TYPE",type);
        DictType dictType = dictTypeMapper.selectOne(queryWrapper);
        return dictType;
    }

    public ResponseMessage getCenterDatas(String id){
        if(StringUtils.isBlank(id)){
            return ResponseMessage.error("选择的树数据为空！");
        }

        DictType dictType = getDciTypeById(id);
        //先查是否是一级（字典类型）
        if(null != dictType){
            DictData dictData = new DictData();
            dictData.setType(dictType.getDictType());
            dictData.setDictTypeText(dictType.getDictName());
            return ResponseMessage.ok(dictData);
        }else{
            //一级（字典类型）为空，查字典数据表
            DictData dictData = dictDataService.getDictDataById(id);
            if(null != dictData){
                //根据TYPE查找名称
                DictType dictType1 = getDciTypeByType(dictData.getType());
                dictData.setDictTypeText(dictType1.getDictName());
            }
            return ResponseMessage.ok(dictData);
        }
    }

    @Transactional
    public ResponseMessage deleteDictype(String id){
        DictType dictType = getDciTypeById(id);
        dictType.setDelFlag(1);
        updateById(dictType);
        //子节点 del_flag设置1
        List<DictData> dictDataList = dictDataService.getDictDataByType(dictType.getDictType());
        for (DictData dictData:dictDataList) {
            dictData.setDelFlag(1);
            dictDataMapper.updateById(dictData);
        }
        return ResponseMessage.ok("删除"+dictType.getDictName()+"成功！");
    }

    /**
      * @method  通过"字典类型"获取字典数据
      * @description
      * @param type : 字典类型
      * @author: xsyuan
      * @return
      */
    public List<DictData> getByType(String type){
        QueryWrapper<DictData> queryWrapper=new QueryWrapper<DictData>()
                .select("ID","DICT_VALUE","DICT_TEXT")
                .eq("TYPE",type)
                .eq("DEL_FLAG",0)
                .orderByAsc("ID");
        List<DictData> list = dictDataMapper.selectList(queryWrapper);
        return list;
    }


}
